﻿using System;
using System.Windows;
using ViewModel.Finance;
using MyWpfApp.Controls;

namespace MyWpfApp.Finance
{
    /// <summary>
    /// Interaction logic for BudgetItemPropertiesView.xaml
    /// </summary>
    public partial class BudgetItemPropertiesView : BasePropertyWindow
    {
        private BudgetItemViewModel viewModel;

        public BudgetItemPropertiesView()
        {
            InitializeComponent();

        }

        private void OKClicked(object sender, RoutedEventArgs e)
        {
            if(!ValidateControlFields()) return;

            if(!viewModel.Save())
            {
                //something bad happend
                return;
            }
            DialogResult = true;

            Close();
        }

        /// <summary>
        /// TODO: need to implement wpf style control validation
        /// </summary>
        /// <returns></returns>
        private bool ValidateControlFields()
        {
            if(txtName.Text == string.Empty)
            {
                txtName.Focus();
                return false;
            }
            return true;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            viewModel = DataContext as BudgetItemViewModel;
        }

        /// <summary>
        /// TODO: move this into a custome TextBox control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtAmount_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
        {
            try
            {
                Convert.ToDecimal(e.Text);
            }
            catch
            {
                e.Handled = true;
            }
        }

    }
}
